var HOME = 'home';
var INSTRUMENTS_DATA = null;
var BATCH_DATA = null;
var gets = new Array();

function adminFunctions() {
	var htmlAdress = document.location.href;
	var entityName = htmlAdress.split("#"); 
	
	if(entityName[1] == null) {
		showTab(HOME);
	} else {
		showTab(entityName[1]);
	}
	
	//adding event listeners to the tabs
	$('#tabs a').click(function(event) {
		showTab(event.currentTarget.id);
	});
	
}

//function to show the tab
var showTab = function(entity) {
	if(entity=="exercise" && gets["instrument"] == null) {
		if(INSTRUMENTS_DATA==null) {
			populateList("instrument",null);
			gets["instrument"] = true;
		} 
		setSelectValue("exercise");
	}
	
	if(entity=="exercise" && gets["batch"] == null) {
		if(BATCH_DATA==null) {
			populateList("batch",null);
			gets["batch"] = true;
		} 
		setSelectValue("batch");
	}
	
	
	if(gets[entity] == null) {
		gets[entity] = true;
		populateList(entity,null);
	}
	console.log("Pokazuje entityName " + entity);
	//remove the active class from all the tabs
	$('.tab').removeClass("active");
	//setting the active class to the selected tab
	$('#'+entity).addClass("active");
	//hiding all the tabs
	$('.g-unit').hide();
	//showing the selected tab
	$('#' + entity + '-tab').show();

	getTab(entity);
	//hiding the create block
//	showHideCreate(entity, false);
}

//function to show/hide create block for an entity in a tab 
var showHideCreate = function(entity, show) {
	//checking if the block is show or not
	if (show) {
		//hiding the search container
		$('#' + entity + '-search-ctr').hide();
		//hiding the list container
		$('#' + entity + '-list-ctr').hide();
		//showing the create container
		$('#' + entity + '-create-ctr').show();
		
	} else {
		//showing the search container
		$('#' + entity + '-search-ctr').show();
		//showing the list container
		$('#' + entity + '-list-ctr').show();
		//hiding the create container
		$('#' + entity + '-create-ctr').hide();
		//checking if the entity is not a home then populating the list of the entity
		//if(entity!=HOME)
			//populateList(entity,null);
	}
}


function getTab(tab) {
	
var formElements = $('form#'+tab+'-form :input');
var formEleList = $('form#'+tab+'-form').serializeArray();

	function updateTips( t ) {
		tips
			.text( t )
			.addClass( "ui-state-highlight" );
		setTimeout(function() {
			tips.removeClass( "ui-state-highlight", 1500 );
		}, 500 );
	}
	
	$( "#"+tab+"-form" ).dialog({
		autoOpen: false,
		height: 300,
		width: 350,
		modal: true,
		buttons: {
			"Dodaj": function() {
				save(tab);				
			},
			Cancel: function() {
				$( this ).dialog( "close" );
			}
		},
		close: function() {
			formElements.val( "" ).removeClass( "ui-state-error" );
		}
	});

	$( "#create-"+tab )
		.button()
		.click(function() {
			$( "#"+tab+"-form" ).dialog( "open" );
		});
}

//parameter object definition
var param=function(name,value){
	this.name=name;
	this.value=value;
}

var save = function(entity) {
	 var data=new Array();
	// collecting the field values from the form
	 var formEleList = $('form#'+entity+'-form').serializeArray();
	 console.log(formEleList);
	 

	 $( "#"+entity+"-table tbody" ).append( "<tr>" );
	 for(var i=0;i<formEleList.length;i++){
		$( "#"+entity+"-table tbody" ).append( "<td>" + formEleList[i].value + "</td>" );
		data[data.length]=new param(formEleList[i].name,formEleList[i].value);
		if(entity=="instrument") {
			if(formEleList[i].name == "title") {
			 $( "#select-exercise" ).append( '<option value="'+formEleList[i].value +'">' + formEleList[i].value + "</option>" );
			}
		}
	 }
	 
	 $( "#"+entity+"-table tbody" ).append( "</tr>" );
	 //setting action as PUT
	 data[data.length]=new param('action','PUT');
	 data[data.length]=new param('gymDataType',entity);
	 
	 console.log(data);
	 //making the ajax call
	 $.ajax({
			url : "/gymdata",
			type : "PUT",
			data:data,
			success  : function(data) {
//				/showHideCreate(entity,false);
			}
		});
	 $('#'+entity+'-reset').click();
	 $('#item-product-list').reset();//='';
}



//function to populate the list of an entity
var populateList=function(entity, filter){
	//specifying the success function. When the ajax response is successful then the following function will be called

	var successFn=function(resp){
		console.log(resp);
		var data='';
		if(resp){
			//getting the data from the response object
			if(entity=="instrument" && INSTRUMENTS_DATA == null) {
				INSTRUMENTS_DATA = resp.data;
				console.log("pobieram isntrument data : " + INSTRUMENTS_DATA);
			}
			if(entity=="batch" && BATCH_DATA == null) {
				BATCH_DATA = resp.data;
				console.log("pobieram isntrument data : " + BATCH_DATA);
			}
			data=resp.data;
		}

		//creating the html content
		
		 var formEleList = $('form#'+entity+'-form').serializeArray();	
		if(data.length > 0 && formEleList.length >0){
			 

			 for (var i=0;i<data.length;i++){	
				 $( "#"+entity+"-table tbody" ).append( "<tr>" );
				 for(var j=0;j<formEleList.length;j++){
					 var ele=$(formEleList[j]);
					 var value = data[i];
					$( "#"+entity+"-table tbody" ).append( "<td>" + eval('value.'+formEleList[j].name) + "</td>" );					
				 }
				 $( "#"+entity+"-table tbody" ).append( "</tr>" );
			 }
			
			
		}
	
		
		//$('#'+entity+'-list-tbody').html(htm);
	}
	getData("/gymdata",filter,successFn,null, entity);
}


//function to get the data by setting url, filter, success function and error function
var getData=function(url,filterData,successFn,errorFn, entity){
	// making the ajax call
	var data=new Array();
	data[data.length]=new param('gymDataType',entity);

	$.ajax({
		url : url,
		type : "GET",
		data: data,
		async: false,
		success : function(resp) {
			//calling the user defined success function
			if(successFn)
			successFn(resp);	
		},
	error:function(e){
		//calling the user defined error function
		if(errorFn)
		 errorFn(e);
	}
	});
}

function setSelectValue(entity) {
	if(entity == "exercise") {
	 for (var i=0;i<INSTRUMENTS_DATA.length;i++){	
		 console.log(INSTRUMENTS_DATA[i].title);
		 $( "#select-"+entity ).append( '<option value="'+INSTRUMENTS_DATA[i].title+'">' + INSTRUMENTS_DATA[i].title + "</option>" );					
	 }
	}
	if(entity == "batch") {
		 for (var i=0;i<BATCH_DATA.length;i++){	
			 console.log(BATCH_DATA[i].title);
			 $( "#select-"+entity ).append( '<option value="'+BATCH_DATA[i].title+'">' + BATCH_DATA[i].title + "</option>" );					
		 }
   }
}	 
	


